import pygame.sprite
from pygame import surface

from actor import WalkDir, DirWalkByImage
from game import DEFAULT_COUNT, DEFAULT_POS_X, DEFAULT_POS_Y, DEFAULT_HP, DEFAULT_NAME, DEFAULT_IMAGE_WIDTH, \
    DEFAULT_IMAGE_HEIGHT

"""
主角类
"""

class XiaoTie(pygame.sprite.Sprite):
    # 生命
    hp = DEFAULT_HP
    # 姓名
    name = DEFAULT_NAME
    # 初始位置
    pos_x = DEFAULT_POS_X
    pos_y = DEFAULT_POS_Y
    image_width = DEFAULT_IMAGE_WIDTH
    image_height = DEFAULT_IMAGE_HEIGHT
    # 地雷数
    boom_count = DEFAULT_COUNT
    # 武器
    weapon = ""

    # 人物状态
    current_status = "boy"
    # 小孩状态
    boy_base_action = None
    # 战士状态
    army_base_action = None
    # 时间戳
    time_stamp = 10
    # 当前方向枚举
    dir = WalkDir.right
    # 子弹数
    shoot_count = 0

    def __init__(self, x, y):
        """
        参数初始化
        :param x:
        :param y:
        """
        super(XiaoTie, self).__init__()
        self.boy_base_action = DirWalkByImage("./resources/images/chinese/militia/boy1.png",
                                              self.image_width, self.image_height, False)
        self.army_base_action = DirWalkByImage("./resources/images/chinese/red_army/boy1.png",
                                               self.image_width, self.image_height, False)

        self.boy_base_action.set_dir(self.dir)
        self.army_base_action.set_dir(self.dir)
        self.current_action = self.boy_base_action
        self.pos_y = y
        self.pos_x = x
        self.font = pygame.font.Font("resources/font/迷你简粗宋.TTF", 15)
        self.time_font = pygame.font.SysFont('SimHei', 40)
        self.mine_font = pygame.font.SysFont('SimHei',25)
        self.bullet_font = pygame.font.SysFont('SimHei',25)
        self.score_font = pygame.font.SysFont('SimHei',25)
        self.rect = pygame.Rect(self.pos_x - x + 10, self.pos_y - y + 10, 20, 45)
        self.score = 0
        self.task3_start = False
        self.time_limit = 1000

    def set_status(self, status: str):
        """
        设置相应状态
        :param status:
        :return:
        """
        if status == "boy":
            self.current_action = self.boy_base_action
        else:
            self.current_action = self.army_base_action

    def run(self, down_flag, key_list, obstacle_group):
        """
        实现主角移动
        :param down_flag: 是否按下按键
        :param key_list: 按键列表
        :param obstacle_group: 障碍物精灵组
        :return:
        """
        self.army_base_action.is_loop = False
        self.boy_base_action.is_loop = False
        off_x = 0
        off_y = 0
        if key_list[pygame.K_s]:
            self.dir = WalkDir.down
            self.army_base_action.is_loop = True
            self.boy_base_action.is_loop = True
            off_y += 7
        if key_list[pygame.K_w]:
            self.dir = WalkDir.up
            self.army_base_action.is_loop = True
            self.boy_base_action.is_loop = True
            off_y -= 7
        if key_list[pygame.K_a]:
            self.dir = WalkDir.left
            self.army_base_action.is_loop = True
            self.boy_base_action.is_loop = True
            off_x -= 7
        if key_list[pygame.K_d]:
            self.dir = WalkDir.right
            self.army_base_action.is_loop = True
            self.boy_base_action.is_loop = True
            off_x += 7
        self.boy_base_action.set_dir(self.dir)
        self.army_base_action.set_dir(self.dir)
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        #  障碍物碰撞检测
        collidate_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(collidate_list1) > 0:
            self.rect = temp_rect
        else:
            self.pos_x += off_x
            self.pos_y += off_y

    def draw(self, surface: pygame.surface, x: int, y: int):
        """
        任务绘制
        :param surface: 视窗图像
        :param x: 视窗左上角x
        :param y: 视窗左上角y
        :return:
        """
        current_image = self.current_action.get_image()
        surface.blit(current_image, (self.pos_x - x, self.pos_y - y))
        word_surface = self.font.render(str(self.hp), None, "red")
        surface.blit(word_surface, (self.pos_x - x + 10, self.pos_y - y))
        pygame.draw.rect(surface, pygame.Color(255, 255, 0),
                         (self.pos_x - x + 10, self.pos_y - y + 10, 20, 45), 1)
        mine_surface = self.mine_font.render("地雷数量："+ str(self.boom_count), True, 'green', None)
        bullet_surface = self.bullet_font.render("子弹数量："+ str(self.shoot_count),True, 'green', None)
        score_surface = self.score_font.render("当前积分："+ str(self.score) + "分", True, 'green', None)
        surface.blit(score_surface, (0, 50))
        surface.blit(mine_surface, (0, 0))
        surface.blit(bullet_surface, (0, 25))
        if self.task3_start:
            current_time = self.time_limit / 10
            time_surface = self.time_font.render("还剩"+str(current_time)+"秒", True, 'red', None)
            print(str(self.pos_x)+","+str(self.pos_y))
            print(str(x)+","+str(y))
            surface.blit(time_surface,(550, 0))



    def set_pos(self, x, y):
        """
        更新当前位置
        :param x:
        :param y:
        :return:
        """
        self.pos_y = y
        self.pos_x = x
        self.rect = pygame.Rect(self.pos_x + 15, self.pos_y + 50, 20, 10)

    def set_count(self, count):
        """
        设置地雷数量
        :param count:
        :return:
        """
        self.boom_count = count
